package activitys;

import utils.BaseAsync;
import utils.PreferencesHelper;

import com.example.pasajero.R;

import controladores.ControladorPasajeros;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.MenuItem;
import android.widget.Toast;

public class BaseActivityCerrarSesion extends Activity
{
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		int id = item.getItemId();
		if (id == R.id.action_settings)
		{
			new CerrarSesion().execute();
		}
		return super.onOptionsItemSelected(item);
	}
	
	private class CerrarSesion extends BaseAsync
	{
		private boolean res;
		
		public CerrarSesion ()
		{
			super (BaseActivityCerrarSesion.this, "Cerrando sesion...");
		}
		
		@Override
		protected Void doInBackground(Void... arg0)
		{
			try
			{
				long id = new PreferencesHelper(getApplicationContext()).leerID();
				
				Log.i("LOG", "ID: " + id);
				
				res = new ControladorPasajeros().cerrarSesion(id);
			}
			catch (Exception e)
			{
				Log.i("LOG", e.getMessage());
			}
			
			return null;
		}
		
		@Override
		protected void onPostExecute(Void result)
		{
			super.onPostExecute(result);
			
			if (res)
			{
				PreferencesHelper helper = new PreferencesHelper(getApplicationContext());
				
				if (helper.eliminarDatosUsuario())
				{
					Intent intent = new Intent (BaseActivityCerrarSesion.this, LoginPasajeroActivity.class);
					startActivity(intent);
				}
			}
			else
				Toast.makeText(BaseActivityCerrarSesion.this, "Fallo la conexion.", Toast.LENGTH_SHORT).show();
		}
	}
}
